package com.example.officialdocumentsys.common;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@WebFilter(filterName = "LoginCheckFilter", urlPatterns = "/*")
public class LoginFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        ThreadLocalContext.setCurrentId(null);

        //匹配URI

        String requestURI = request.getRequestURI();
        log.info("拦截到请求{}",requestURI);
        String[] urls = new String[]{
                "/getKaptchaImage",
                "/front/**",
                "/common/**",
                "/user/logout",
                "/user/login",
                "/user/forget",
                "/user/alterPassword",
                "/user/register",
                "/category/page",
                "/book/pagePlus",
                "/category/list/",
                "/favicon.ico",
                "/admin/login",
                "/**"
        };
        boolean check = check(urls, requestURI);

        //判断是否需要拦截处理
        if(check){
            log.info("请求{}无需处理",requestURI);
            if(request.getSession().getAttribute("user") != null){
                Long userId = (Long)request.getSession().getAttribute("user");
                ThreadLocalContext.setCurrentId(userId);
            }

            if(request.getSession().getAttribute("admin") != null){
                Long adminId = (Long)request.getSession().getAttribute("admin");
                ThreadLocalContext.setCurrentId(adminId);
            }
            filterChain.doFilter(request, response);


            return;
        }

        //判断登入状态，若已登入则放行
        if(request.getSession().getAttribute("admin") != null){
            log.info("已经登入",request.getSession().getAttribute("admin"));

            Long adminId = (Long)request.getSession().getAttribute("admin");

            ThreadLocalContext.setCurrentId(adminId);

            filterChain.doFilter(request, response);
            return;
        }

        //判断客户状态，若已登入则放行
        if(request.getSession().getAttribute("user") != null){
            log.info("已经登入",request.getSession().getAttribute("user"));

            Long userId = (Long)request.getSession().getAttribute("user");
            ThreadLocalContext.setCurrentId(userId);

            filterChain.doFilter(request, response);
            return;
        }
        //未登录
        log.info("未登录");
        response.getWriter().write(JSON.toJSONString(R.error("未登录")));
        return;
    }

    @Override
    public void destroy() {

    }

    public boolean check(String[] urls, String requestURI){
        for(String url:urls){
            boolean match = PATH_MATCHER.match(url, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }

}
